package com.zj.tool;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateTools {

    public static Date toDateTimeFromStr(String dateTimeStr) {

        // 创建DateTimeFormatter对象，指定日期时间的格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 解析字符串到LocalDateTime对象
        LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr, formatter);

        // 将LocalDateTime转换为java.util.Date对象
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        return date;

    }

    public static Date toDateFromStr(String dateTimeStr) {

        // 创建DateTimeFormatter对象，指定日期时间的格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 解析字符串到LocalDateTime对象
        LocalDate localDateTime = LocalDate.parse(dateTimeStr, formatter);

        // 将LocalDate转换为java.util.Date对象
        Date date = Date.from(localDateTime.atStartOfDay(ZoneId.systemDefault()).toInstant());
        return date;

    }

    public static void main(String[] args) {
        String str="2020-04-01";
        System.out.println(toDateFromStr(str));
    }
}
